package com.youlu.campus.service.commission;

import com.youlu.campus.base.exception.BusinessException;
import com.youlu.campus.entity.ActivityInfo;
import com.youlu.campus.entity.commission.SharingCommissionInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Slf4j
@Service
public class ActivitySharingCommissionProfitService {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分账比例不能为0
     */
    private final Double ZERO = 0d;
    /**
     * 分账比例不能超过60%
     */
    private final Double MAX_RATIO = 60d;

    public static void main(String[] args) {
        LocalDateTime localDateTime = LocalDateTime.now().plusHours(12L);
        System.out.println(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    }


    public SharingCommissionInfo getSharingProfitConfigByActivityId(String activityId) {
        ActivityInfo activityInfo = this.mongoTemplate.findById(activityId, ActivityInfo.class);
        if (Objects.isNull(activityInfo)) {
            new BusinessException("活动不存在");
        }
        SharingCommissionInfo merchantSharingProfit = activityInfo.getSharingCommissionInfo();
        if (Objects.isNull(merchantSharingProfit)) {
            return new SharingCommissionInfo();
        }
        return merchantSharingProfit;
    }

    public void saveSharingProfitConfig(String activityId, SharingCommissionInfo sharingCommissionInfo) {
        if (Objects.isNull(sharingCommissionInfo)) {
            return;
        }
        ActivityInfo activityInfo = this.mongoTemplate.findById(activityId, ActivityInfo.class);
        if (Objects.isNull(activityInfo)) {
            return;
        }
        activityInfo.setSharingCommissionInfo(sharingCommissionInfo);
        this.mongoTemplate.save(activityInfo);
    }


}
